

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="zh" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="zh" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>常见问题 &mdash; Optuna 1.4.0 文档</title>
  

  
  
    <link rel="shortcut icon" href="_static/favicon.ico"/>
  
  
  

  
  <script type="text/javascript" src="_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
        <script src="_static/jquery.js"></script>
        <script src="_static/underscore.js"></script>
        <script src="_static/doctools.js"></script>
        <script src="_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="_static/css/custom.css" type="text/css" />
    <link rel="index" title="索引" href="genindex.html" />
    <link rel="search" title="搜索" href="search.html" />
    <link rel="prev" title="Visualization" href="reference/visualization.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="index.html" class="icon icon-home"> Optuna
          

          
          </a>

          
            
            
              <div class="version">
                1.4.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">目录</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="installation.html">安装</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial/index.html">教程</a></li>
<li class="toctree-l1"><a class="reference internal" href="reference/index.html">API Reference</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">常见问题</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#can-i-use-optuna-with-x-where-x-is-your-favorite-ml-library">某某库可以和 Optuna 配合使用吗？（某某是你常用的机器学习库）</a></li>
<li class="toctree-l2"><a class="reference internal" href="#how-to-define-objective-functions-that-have-own-arguments">如何定义带有额外参数的目标函数？</a></li>
<li class="toctree-l2"><a class="reference internal" href="#can-i-use-optuna-without-remote-rdb-servers">没有远程 RDB 的情况下可以使用 Optuna 吗？</a></li>
<li class="toctree-l2"><a class="reference internal" href="#how-can-i-save-and-resume-studies">如何保存和恢复 study？</a></li>
<li class="toctree-l2"><a class="reference internal" href="#how-to-suppress-log-messages-of-optuna">如何禁用 Optuna 的日志信息？</a></li>
<li class="toctree-l2"><a class="reference internal" href="#how-to-save-machine-learning-models-trained-in-objective-functions">如何在目标函数中保存训练好的机器学习模型？</a></li>
<li class="toctree-l2"><a class="reference internal" href="#how-can-i-obtain-reproducible-optimization-results">如何获得可复现的优化结果？</a></li>
<li class="toctree-l2"><a class="reference internal" href="#how-are-exceptions-from-trials-handled">Trial 是如何处理抛出异常的？</a></li>
<li class="toctree-l2"><a class="reference internal" href="#how-are-nans-returned-by-trials-handled">Trial 返回的 NaN 是如何处理的？</a></li>
<li class="toctree-l2"><a class="reference internal" href="#what-happens-when-i-dynamically-alter-a-search-space">动态地改变搜索空间会导致怎样的结果？</a></li>
<li class="toctree-l2"><a class="reference internal" href="#how-can-i-use-two-gpus-for-evaluating-two-trials-simultaneously">如何在两块 GPU 上同时跑两个 trial?</a></li>
<li class="toctree-l2"><a class="reference internal" href="#how-can-i-test-my-objective-functions">如何对目标函数进行测试？</a></li>
</ul>
</li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="index.html">Optuna</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="index.html">Docs</a> &raquo;</li>
        
      <li>常见问题</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
            <a href="_sources/faq.rst.txt" rel="nofollow"> View page source</a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="faq">
<h1>常见问题<a class="headerlink" href="#faq" title="永久链接至标题">¶</a></h1>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><p><a class="reference internal" href="#can-i-use-optuna-with-x-where-x-is-your-favorite-ml-library" id="id1">某某库可以和 Optuna 配合使用吗？（某某是你常用的机器学习库）</a></p></li>
<li><p><a class="reference internal" href="#how-to-define-objective-functions-that-have-own-arguments" id="id2">如何定义带有额外参数的目标函数？</a></p></li>
<li><p><a class="reference internal" href="#can-i-use-optuna-without-remote-rdb-servers" id="id3">没有远程 RDB 的情况下可以使用 Optuna 吗？</a></p></li>
<li><p><a class="reference internal" href="#how-can-i-save-and-resume-studies" id="id4">如何保存和恢复 study？</a></p></li>
<li><p><a class="reference internal" href="#how-to-suppress-log-messages-of-optuna" id="id5">如何禁用 Optuna 的日志信息？</a></p></li>
<li><p><a class="reference internal" href="#how-to-save-machine-learning-models-trained-in-objective-functions" id="id6">如何在目标函数中保存训练好的机器学习模型？</a></p></li>
<li><p><a class="reference internal" href="#how-can-i-obtain-reproducible-optimization-results" id="id7">如何获得可复现的优化结果？</a></p></li>
<li><p><a class="reference internal" href="#how-are-exceptions-from-trials-handled" id="id8">Trial 是如何处理抛出异常的？</a></p></li>
<li><p><a class="reference internal" href="#how-are-nans-returned-by-trials-handled" id="id9">Trial 返回的 NaN 是如何处理的？</a></p></li>
<li><p><a class="reference internal" href="#what-happens-when-i-dynamically-alter-a-search-space" id="id10">动态地改变搜索空间会导致怎样的结果？</a></p></li>
<li><p><a class="reference internal" href="#how-can-i-use-two-gpus-for-evaluating-two-trials-simultaneously" id="id11">如何在两块 GPU 上同时跑两个 trial?</a></p></li>
<li><p><a class="reference internal" href="#how-can-i-test-my-objective-functions" id="id12">如何对目标函数进行测试？</a></p></li>
</ul>
</div>
<div class="section" id="can-i-use-optuna-with-x-where-x-is-your-favorite-ml-library">
<h2><a class="toc-backref" href="#id1">某某库可以和 Optuna 配合使用吗？（某某是你常用的机器学习库）</a><a class="headerlink" href="#can-i-use-optuna-with-x-where-x-is-your-favorite-ml-library" title="永久链接至标题">¶</a></h2>
<p>Optuna 和绝大多数机器学习库兼容，并且很容易同他们配合使用。参见 <a class="reference external" href="https://github.com/optuna/optuna/tree/master/examples">examples</a>.</p>
</div>
<div class="section" id="how-to-define-objective-functions-that-have-own-arguments">
<span id="objective-func-additional-args"></span><h2><a class="toc-backref" href="#id2">如何定义带有额外参数的目标函数？</a><a class="headerlink" href="#how-to-define-objective-functions-that-have-own-arguments" title="永久链接至标题">¶</a></h2>
<p>有两种方法可以实现这类函数。</p>
<p>首先，如下例所示，可调用的 objective 类具有这个功能：</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">optuna</span>

<span class="k">class</span> <span class="nc">Objective</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">min_x</span><span class="p">,</span> <span class="n">max_x</span><span class="p">):</span>
        <span class="c1"># Hold this implementation specific arguments as the fields of the class.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">min_x</span> <span class="o">=</span> <span class="n">min_x</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_x</span> <span class="o">=</span> <span class="n">max_x</span>

    <span class="k">def</span> <span class="fm">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial</span><span class="p">):</span>
        <span class="c1"># Calculate an objective value by using the extra arguments.</span>
        <span class="n">x</span> <span class="o">=</span> <span class="n">trial</span><span class="o">.</span><span class="n">suggest_uniform</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">min_x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_x</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>

<span class="c1"># Execute an optimization by using an `Objective` instance.</span>
<span class="n">study</span> <span class="o">=</span> <span class="n">optuna</span><span class="o">.</span><span class="n">create_study</span><span class="p">()</span>
<span class="n">study</span><span class="o">.</span><span class="n">optimize</span><span class="p">(</span><span class="n">Objective</span><span class="p">(</span><span class="o">-</span><span class="mi">100</span><span class="p">,</span> <span class="mi">100</span><span class="p">),</span> <span class="n">n_trials</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
</pre></div>
</div>
<p>其次，你可以用 <code class="docutils literal notranslate"><span class="pre">lambda</span></code> 或者 <code class="docutils literal notranslate"><span class="pre">functools.partial</span></code> 来创建带有额外参数的函数（闭包）。 下面是一个使用了 <code class="docutils literal notranslate"><span class="pre">lambda</span></code> 的例子：</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">optuna</span>

<span class="c1"># Objective function that takes three arguments.</span>
<span class="k">def</span> <span class="nf">objective</span><span class="p">(</span><span class="n">trial</span><span class="p">,</span> <span class="n">min_x</span><span class="p">,</span> <span class="n">max_x</span><span class="p">):</span>
    <span class="n">x</span> <span class="o">=</span> <span class="n">trial</span><span class="o">.</span><span class="n">suggest_uniform</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="n">min_x</span><span class="p">,</span> <span class="n">max_x</span><span class="p">)</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>

<span class="c1"># Extra arguments.</span>
<span class="n">min_x</span> <span class="o">=</span> <span class="o">-</span><span class="mi">100</span>
<span class="n">max_x</span> <span class="o">=</span> <span class="mi">100</span>

<span class="c1"># Execute an optimization by using the above objective function wrapped by `lambda`.</span>
<span class="n">study</span> <span class="o">=</span> <span class="n">optuna</span><span class="o">.</span><span class="n">create_study</span><span class="p">()</span>
<span class="n">study</span><span class="o">.</span><span class="n">optimize</span><span class="p">(</span><span class="k">lambda</span> <span class="n">trial</span><span class="p">:</span> <span class="n">objective</span><span class="p">(</span><span class="n">trial</span><span class="p">,</span> <span class="n">min_x</span><span class="p">,</span> <span class="n">max_x</span><span class="p">),</span> <span class="n">n_trials</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
</pre></div>
</div>
<p>其他例子参见 <a class="reference external" href="https://github.com/optuna/optuna/blob/master/examples/sklearn_additional_args.py">sklearn_addtitional_args.py</a> .</p>
</div>
<div class="section" id="can-i-use-optuna-without-remote-rdb-servers">
<h2><a class="toc-backref" href="#id3">没有远程 RDB 的情况下可以使用 Optuna 吗？</a><a class="headerlink" href="#can-i-use-optuna-without-remote-rdb-servers" title="永久链接至标题">¶</a></h2>
<p>可以。</p>
<p>在最简单的情况下，Optuna 使用内存 (in-memory) 存储：</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">study</span> <span class="o">=</span> <span class="n">optuna</span><span class="o">.</span><span class="n">create_study</span><span class="p">()</span>
<span class="n">study</span><span class="o">.</span><span class="n">optimize</span><span class="p">(</span><span class="n">objective</span><span class="p">)</span>
</pre></div>
</div>
<p>如果想保存和恢复 study 的话，你可以轻松地将 SQLite 用作本地存储。</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">study</span> <span class="o">=</span> <span class="n">optuna</span><span class="o">.</span><span class="n">create_study</span><span class="p">(</span><span class="n">study_name</span><span class="o">=</span><span class="s1">&#39;foo_study&#39;</span><span class="p">,</span> <span class="n">storage</span><span class="o">=</span><span class="s1">&#39;sqlite:///example.db&#39;</span><span class="p">)</span>
<span class="n">study</span><span class="o">.</span><span class="n">optimize</span><span class="p">(</span><span class="n">objective</span><span class="p">)</span>  <span class="c1"># The state of `study` will be persisted to the local SQLite file.</span>
</pre></div>
</div>
<p>更多细节请参考 <a class="reference internal" href="tutorial/rdb.html#rdb"><span class="std std-ref">用 RDB 后端保存/恢复 Study</span></a>.</p>
</div>
<div class="section" id="how-can-i-save-and-resume-studies">
<h2><a class="toc-backref" href="#id4">如何保存和恢复 study？</a><a class="headerlink" href="#how-can-i-save-and-resume-studies" title="永久链接至标题">¶</a></h2>
<p>有两种方法可以将 study 持久化。具体采用哪种取决于你是使用内存存储 (in-memory) 还是远程数据库存储 (RDB).通过 <code class="docutils literal notranslate"><span class="pre">pickle</span></code> 或者 <code class="docutils literal notranslate"><span class="pre">joblib</span></code>, 采用了内存存储的 study 可以和普通的 Python 对象一样被存储和加载。比如用 <code class="docutils literal notranslate"><span class="pre">joblib</span></code> 的话：</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">study</span> <span class="o">=</span> <span class="n">optuna</span><span class="o">.</span><span class="n">create_study</span><span class="p">()</span>
<span class="n">joblib</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">study</span><span class="p">,</span> <span class="s1">&#39;study.pkl&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>恢复 study:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">study</span> <span class="o">=</span> <span class="n">joblib</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="s1">&#39;study.pkl&#39;</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Best trial until now:&#39;</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39; Value: &#39;</span><span class="p">,</span> <span class="n">study</span><span class="o">.</span><span class="n">best_trial</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39; Params: &#39;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">study</span><span class="o">.</span><span class="n">best_trial</span><span class="o">.</span><span class="n">params</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
    <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;    </span><span class="si">{</span><span class="n">key</span><span class="si">}</span><span class="s1">: </span><span class="si">{</span><span class="n">value</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>如果你用的是 RDB, 具体细节请参考 <a class="reference internal" href="tutorial/rdb.html#rdb"><span class="std std-ref">用 RDB 后端保存/恢复 Study</span></a>.</p>
</div>
<div class="section" id="how-to-suppress-log-messages-of-optuna">
<h2><a class="toc-backref" href="#id5">如何禁用 Optuna 的日志信息？</a><a class="headerlink" href="#how-to-suppress-log-messages-of-optuna" title="永久链接至标题">¶</a></h2>
<p>默认情况下，Optuna 打印处于 <code class="docutils literal notranslate"><span class="pre">optuna.logging.INFO</span></code> 层级的日志信息。通过设置  <a class="reference internal" href="reference/logging.html#optuna.logging.set_verbosity" title="optuna.logging.set_verbosity"><code class="xref py py-func docutils literal notranslate"><span class="pre">optuna.logging.set_verbosity()</span></code></a>, 你可以改变这个层级。</p>
<p>比如，下面的代码可以终止打印每一个trial的结果：</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">optuna</span><span class="o">.</span><span class="n">logging</span><span class="o">.</span><span class="n">set_verbosity</span><span class="p">(</span><span class="n">optuna</span><span class="o">.</span><span class="n">logging</span><span class="o">.</span><span class="n">WARNING</span><span class="p">)</span>

<span class="n">study</span> <span class="o">=</span> <span class="n">optuna</span><span class="o">.</span><span class="n">create_study</span><span class="p">()</span>
<span class="n">study</span><span class="o">.</span><span class="n">optimize</span><span class="p">(</span><span class="n">objective</span><span class="p">)</span>
<span class="c1"># Logs like &#39;[I 2018-12-05 11:41:42,324] Finished a trial resulted in value:...&#39; are disabled.</span>
</pre></div>
</div>
<p>更多的细节请参考  <a class="reference internal" href="reference/logging.html#module-optuna.logging" title="optuna.logging"><code class="xref py py-class docutils literal notranslate"><span class="pre">optuna.logging</span></code></a>.</p>
</div>
<div class="section" id="how-to-save-machine-learning-models-trained-in-objective-functions">
<h2><a class="toc-backref" href="#id6">如何在目标函数中保存训练好的机器学习模型？</a><a class="headerlink" href="#how-to-save-machine-learning-models-trained-in-objective-functions" title="永久链接至标题">¶</a></h2>
<p>Optuna 会保存超参数和对应的目标函数值，但是它不会存储诸如机器学习模型或者网络权重这样的中间数据。要保存模型或者权重的话，请利用你正在使用的机器学习库提供的对应功能。</p>
<p>在保存模型的时候，我们推荐将 <a class="reference internal" href="reference/trial.html#optuna.trial.Trial.number" title="optuna.trial.Trial.number"><code class="xref py py-obj docutils literal notranslate"><span class="pre">optuna.trial.Trial.number</span></code></a> 一同存储。这样易于之后确认对应的 trial.比如，你可以用以下方式在目标函数中保存训练好的 SVM 模型：</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">objective</span><span class="p">(</span><span class="n">trial</span><span class="p">):</span>
    <span class="n">svc_c</span> <span class="o">=</span> <span class="n">trial</span><span class="o">.</span><span class="n">suggest_loguniform</span><span class="p">(</span><span class="s1">&#39;svc_c&#39;</span><span class="p">,</span> <span class="mf">1e-10</span><span class="p">,</span> <span class="mf">1e10</span><span class="p">)</span>
    <span class="n">clf</span> <span class="o">=</span> <span class="n">sklearn</span><span class="o">.</span><span class="n">svm</span><span class="o">.</span><span class="n">SVC</span><span class="p">(</span><span class="n">C</span><span class="o">=</span><span class="n">svc_c</span><span class="p">)</span>
    <span class="n">clf</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X_train</span><span class="p">,</span> <span class="n">y_train</span><span class="p">)</span>

    <span class="c1"># Save a trained model to a file.</span>
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">.pickle&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">trial</span><span class="o">.</span><span class="n">number</span><span class="p">),</span> <span class="s1">&#39;wb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fout</span><span class="p">:</span>
        <span class="n">pickle</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">clf</span><span class="p">,</span> <span class="n">fout</span><span class="p">)</span>
    <span class="k">return</span> <span class="mf">1.0</span> <span class="o">-</span> <span class="n">accuracy_score</span><span class="p">(</span><span class="n">y_valid</span><span class="p">,</span> <span class="n">clf</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">X_valid</span><span class="p">))</span>


<span class="n">study</span> <span class="o">=</span> <span class="n">optuna</span><span class="o">.</span><span class="n">create_study</span><span class="p">()</span>
<span class="n">study</span><span class="o">.</span><span class="n">optimize</span><span class="p">(</span><span class="n">objective</span><span class="p">,</span> <span class="n">n_trials</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>

<span class="c1"># Load the best model.</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">.pickle&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">study</span><span class="o">.</span><span class="n">best_trial</span><span class="o">.</span><span class="n">number</span><span class="p">),</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fin</span><span class="p">:</span>
    <span class="n">best_clf</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">fin</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">accuracy_score</span><span class="p">(</span><span class="n">y_valid</span><span class="p">,</span> <span class="n">best_clf</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">X_valid</span><span class="p">)))</span>
</pre></div>
</div>
</div>
<div class="section" id="how-can-i-obtain-reproducible-optimization-results">
<h2><a class="toc-backref" href="#id7">如何获得可复现的优化结果？</a><a class="headerlink" href="#how-can-i-obtain-reproducible-optimization-results" title="永久链接至标题">¶</a></h2>
<p>要让 Optuna 生成的参数可复现的话，你可以通过设置 <a class="reference internal" href="reference/samplers.html#optuna.samplers.RandomSampler" title="optuna.samplers.RandomSampler"><code class="xref py py-class docutils literal notranslate"><span class="pre">RandomSampler</span></code></a> 或者 <a class="reference internal" href="reference/samplers.html#optuna.samplers.TPESampler" title="optuna.samplers.TPESampler"><code class="xref py py-class docutils literal notranslate"><span class="pre">TPESampler</span></code></a> 中的参数 <code class="docutils literal notranslate"><span class="pre">seed</span></code> 来指定一个固定的随机数种子：</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">sampler</span> <span class="o">=</span> <span class="n">TPESampler</span><span class="p">(</span><span class="n">seed</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>  <span class="c1"># Make the sampler behave in a deterministic way.</span>
<span class="n">study</span> <span class="o">=</span> <span class="n">optuna</span><span class="o">.</span><span class="n">create_study</span><span class="p">(</span><span class="n">sampler</span><span class="o">=</span><span class="n">sampler</span><span class="p">)</span>
<span class="n">study</span><span class="o">.</span><span class="n">optimize</span><span class="p">(</span><span class="n">objective</span><span class="p">)</span>
</pre></div>
</div>
<p>但是这么做的需要注意以下两点。</p>
<p>首先，如果一个 study 的优化过程本身是分布式的或者并行的，那么这个过程中存在着固有的不确定性。因此，在这种情况下我们很难复现出同样的结果。如果你想复现结果的话，我们建议用顺序执行的方式来优化你的 study.</p>
<p>其次，如果你的目标函数的行为本身就是不确定的（也就是说，即使送入同样的参数，其返回值也不是唯一的），那么你就无法复现这个优化过程。要解决这个问题的话，请设置一个选项（比如随机数种子）来让你的优化目标的行为变成确定性的，前提是你用的机器学习库支持这一功能。</p>
</div>
<div class="section" id="how-are-exceptions-from-trials-handled">
<h2><a class="toc-backref" href="#id8">Trial 是如何处理抛出异常的？</a><a class="headerlink" href="#how-are-exceptions-from-trials-handled" title="永久链接至标题">¶</a></h2>
<p>那些抛出异常却没有对应的捕获机制的 trial 会被视作失败的 trial, 也就是处于 <a class="reference internal" href="reference/trial.html#optuna.trial.TrialState.FAIL" title="optuna.trial.TrialState.FAIL"><code class="xref py py-obj docutils literal notranslate"><span class="pre">FAIL</span></code></a> 状态的 trial.</p>
<p>在默认情况下，除了目标函数中抛出的 <a class="reference internal" href="reference/exceptions.html#optuna.exceptions.TrialPruned" title="optuna.exceptions.TrialPruned"><code class="xref py py-class docutils literal notranslate"><span class="pre">TrialPruned</span></code></a>, 其他所有异常都会被传回给调用函数 <a class="reference internal" href="reference/study.html#optuna.study.Study.optimize" title="optuna.study.Study.optimize"><code class="xref py py-func docutils literal notranslate"><span class="pre">optimize()</span></code></a>.换句话说，当此类异常被抛出时，对应的 study 就会被终止。但有时候我们希望能用剩余的 trial 将该 study 继续下去。要这么做的话，你得通过 <a class="reference internal" href="reference/study.html#optuna.study.Study.optimize" title="optuna.study.Study.optimize"><code class="xref py py-func docutils literal notranslate"><span class="pre">optimize()</span></code></a> 函数中的 <code class="docutils literal notranslate"><span class="pre">catch</span></code> 参数来指定要捕获的异常类型。这样，此类异常就会在 study 内部被捕获，而不会继续向外层传递。</p>
<p>你可以在日志信息里找到失败的 trial.</p>
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span><span class="o">[</span>W <span class="m">2018</span>-12-07 <span class="m">16</span>:38:36,889<span class="o">]</span> Setting status of trial#0 as TrialState.FAIL because of <span class="se">\</span>
the following error: ValueError<span class="o">(</span><span class="s1">&#39;A sample error in objective.&#39;</span><span class="o">)</span>
</pre></div>
</div>
<p>你也可以通过查看 trial 的状态来找到它们：</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">study</span><span class="o">.</span><span class="n">trials_dataframe</span><span class="p">()</span>
</pre></div>
</div>
<table class="docutils align-default">
<colgroup>
<col style="width: 17%" />
<col style="width: 17%" />
<col style="width: 17%" />
<col style="width: 17%" />
<col style="width: 17%" />
<col style="width: 17%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p>number</p></td>
<td><p>state</p></td>
<td><p>value</p></td>
<td><p>...</p></td>
<td><p>params</p></td>
<td><p>system_attrs</p></td>
</tr>
<tr class="row-even"><td><p>0</p></td>
<td><p>TrialState.FAIL</p></td>
<td></td>
<td><p>...</p></td>
<td><p>0</p></td>
<td><p>Setting status of trial#0 as TrialState.FAIL because of the following error: ValueError('A test error in objective.')</p></td>
</tr>
<tr class="row-odd"><td><p>1</p></td>
<td><p>TrialState.COMPLETE</p></td>
<td><p>1269</p></td>
<td><p>...</p></td>
<td><p>1</p></td>
<td></td>
</tr>
</tbody>
</table>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<p>The <code class="docutils literal notranslate"><span class="pre">catch</span></code> argument in <a class="reference internal" href="reference/study.html#optuna.study.Study.optimize" title="optuna.study.Study.optimize"><code class="xref py py-func docutils literal notranslate"><span class="pre">optimize()</span></code></a>.</p>
</div>
</div>
<div class="section" id="how-are-nans-returned-by-trials-handled">
<h2><a class="toc-backref" href="#id9">Trial 返回的 NaN 是如何处理的？</a><a class="headerlink" href="#how-are-nans-returned-by-trials-handled" title="永久链接至标题">¶</a></h2>
<p>返回 <code class="xref py py-obj docutils literal notranslate"><span class="pre">NaN</span></code> 的 trial 被视为失败的 trial, 但是它们并不会导致 study 被终止。</p>
<p>这些返回  <code class="xref py py-obj docutils literal notranslate"><span class="pre">NaN</span></code> 的 trial 在日志里长这样：</p>
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span><span class="o">[</span>W <span class="m">2018</span>-12-07 <span class="m">16</span>:41:59,000<span class="o">]</span> Setting status of trial#2 as TrialState.FAIL because the <span class="se">\</span>
objective <span class="k">function</span> returned nan.
</pre></div>
</div>
</div>
<div class="section" id="what-happens-when-i-dynamically-alter-a-search-space">
<h2><a class="toc-backref" href="#id10">动态地改变搜索空间会导致怎样的结果？</a><a class="headerlink" href="#what-happens-when-i-dynamically-alter-a-search-space" title="永久链接至标题">¶</a></h2>
<p>由于参数空间只在调用 suggestion API (比如 <a class="reference internal" href="reference/trial.html#optuna.trial.Trial.suggest_uniform" title="optuna.trial.Trial.suggest_uniform"><code class="xref py py-func docutils literal notranslate"><span class="pre">suggest_uniform()</span></code></a> 和 <a class="reference internal" href="reference/trial.html#optuna.trial.Trial.suggest_int" title="optuna.trial.Trial.suggest_int"><code class="xref py py-func docutils literal notranslate"><span class="pre">suggest_int()</span></code></a>) 的时候才会被确定，因此，即使在同一个 study 中，我们也可以通过在不同 trial 里对不同的参数空间进行采样来实现对搜索空间的改变。参数空间改变之后的行为是由 sampler 来决定的。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>关于 TPE sampler 的 讨论：<a class="reference external" href="https://github.com/optuna/optuna/issues/822">https://github.com/optuna/optuna/issues/822</a></p>
</div>
</div>
<div class="section" id="how-can-i-use-two-gpus-for-evaluating-two-trials-simultaneously">
<h2><a class="toc-backref" href="#id11">如何在两块 GPU 上同时跑两个 trial?</a><a class="headerlink" href="#how-can-i-use-two-gpus-for-evaluating-two-trials-simultaneously" title="永久链接至标题">¶</a></h2>
<p>如果你的优化目标支持 GPU (CUDA) 加速，你又想指定优化所用的 GPU 的话，设置 <code class="docutils literal notranslate"><span class="pre">CUDA_VISIBLE_DEVICES</span></code> 环境变量可能是实现这一目标最轻松的方式了：</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="c1"># On a terminal.</span>
<span class="c1">#</span>
<span class="c1"># Specify to use the first GPU, and run an optimization.</span>
$ <span class="nb">export</span> <span class="nv">CUDA_VISIBLE_DEVICES</span><span class="o">=</span><span class="m">0</span>
$ optuna study optimize foo.py objective --study-name foo --storage sqlite:///example.db

<span class="c1"># On another terminal.</span>
<span class="c1">#</span>
<span class="c1"># Specify to use the second GPU, and run another optimization.</span>
$ <span class="nb">export</span> <span class="nv">CUDA_VISIBLE_DEVICES</span><span class="o">=</span><span class="m">1</span>
$ optuna study optimize bar.py objective --study-name bar --storage sqlite:///example.db
</pre></div>
</div>
<p>更多细节见 <a class="reference external" href="https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#env-vars">CUDA C Programming Guide</a>.</p>
</div>
<div class="section" id="how-can-i-test-my-objective-functions">
<h2><a class="toc-backref" href="#id12">如何对目标函数进行测试？</a><a class="headerlink" href="#how-can-i-test-my-objective-functions" title="永久链接至标题">¶</a></h2>
<p>在对目标函数的测试中，我们总倾向于使用固定的，而不是随机采样的参数。这时，你可以选择用 <a class="reference internal" href="reference/trial.html#optuna.trial.FixedTrial" title="optuna.trial.FixedTrial"><code class="xref py py-class docutils literal notranslate"><span class="pre">FixedTrial</span></code></a> 作为目标函数的输入参数。它会从一个给定的参数字典中送入固定的参数值。比如，针对函数 <span class="math notranslate nohighlight">\(x + y\)</span>, 你可以用如下方式送入两个任意的 <span class="math notranslate nohighlight">\(x\)</span> 和 <span class="math notranslate nohighlight">\(y\)</span>:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">objective</span><span class="p">(</span><span class="n">trial</span><span class="p">):</span>
    <span class="n">x</span> <span class="o">=</span> <span class="n">trial</span><span class="o">.</span><span class="n">suggest_uniform</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)</span>
    <span class="n">y</span> <span class="o">=</span> <span class="n">trial</span><span class="o">.</span><span class="n">suggest_int</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">,</span> <span class="o">-</span><span class="mi">5</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span>

<span class="n">objective</span><span class="p">(</span><span class="n">FixedTrial</span><span class="p">({</span><span class="s1">&#39;x&#39;</span><span class="p">:</span> <span class="mf">1.0</span><span class="p">,</span> <span class="s1">&#39;y&#39;</span><span class="p">:</span> <span class="o">-</span><span class="mi">1</span><span class="p">}))</span>  <span class="c1"># 0.0</span>
<span class="n">objective</span><span class="p">(</span><span class="n">FixedTrial</span><span class="p">({</span><span class="s1">&#39;x&#39;</span><span class="p">:</span> <span class="o">-</span><span class="mf">1.0</span><span class="p">,</span> <span class="s1">&#39;y&#39;</span><span class="p">:</span> <span class="o">-</span><span class="mi">4</span><span class="p">}))</span>  <span class="c1"># -5.0</span>
</pre></div>
</div>
<p>如果使用 <a class="reference internal" href="reference/trial.html#optuna.trial.FixedTrial" title="optuna.trial.FixedTrial"><code class="xref py py-class docutils literal notranslate"><span class="pre">FixedTrial</span></code></a> 的话，你也可以用如下方式写单元测试：</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># A test function of pytest</span>
<span class="k">def</span> <span class="nf">test_objective</span><span class="p">():</span>
    <span class="k">assert</span> <span class="mf">1.0</span> <span class="o">==</span> <span class="n">objective</span><span class="p">(</span><span class="n">FixedTrial</span><span class="p">({</span><span class="s1">&#39;x&#39;</span><span class="p">:</span> <span class="mf">1.0</span><span class="p">,</span> <span class="s1">&#39;y&#39;</span><span class="p">:</span> <span class="mi">0</span><span class="p">}))</span>
    <span class="k">assert</span> <span class="o">-</span><span class="mf">1.0</span> <span class="o">==</span> <span class="n">objective</span><span class="p">(</span><span class="n">FixedTrial</span><span class="p">({</span><span class="s1">&#39;x&#39;</span><span class="p">:</span> <span class="mf">0.0</span><span class="p">,</span> <span class="s1">&#39;y&#39;</span><span class="p">:</span> <span class="o">-</span><span class="mi">1</span><span class="p">}))</span>
    <span class="k">assert</span> <span class="mf">0.0</span> <span class="o">==</span> <span class="n">objective</span><span class="p">(</span><span class="n">FixedTrial</span><span class="p">({</span><span class="s1">&#39;x&#39;</span><span class="p">:</span> <span class="o">-</span><span class="mf">1.0</span><span class="p">,</span> <span class="s1">&#39;y&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">}))</span>
</pre></div>
</div>
</div>
</div>


           </div>
           
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
      
        <a href="reference/visualization.html" class="btn btn-neutral float-left" title="Visualization" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, Optuna Contributors.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
    <a href="privacy.html">Privacy Policy</a>.
     


</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
    <!-- Theme Analytics -->
    <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-55135190-8', 'auto');
    ga('send', 'pageview');
    </script>

    
   

</body>
</html>